<?php
namespace app\api\controller\v1;
use app\common\logic\TechnLevelLogic;
use think\Db;
use app\service\OrderService;
use app\service\TechnService;


class Schedule extends Base
{
	public function _initialize()
	{
		parent::_initialize();
		
	}

	/**
	 * 用户信息
	 */
	public function index()
	{
	    $day=date('d');
        if ($day == 1 || $day == 16){
            $list=db::name('proxy')->where('is_delete_time',0)->select();
        }else{
            $list = null;
        }
	    if(!empty($list)){
	        foreach ($list as $k=>$v){
	            $techn=db::name('techn')->where('city',$v['shi'])->select();
	            foreach($techn as $k2=>$v2){
	                $techn_last_info = Db::name('techn_last')->where(['techn_id' => $v2['id']])->find();
                    //开始计算加钟率 需要techn_id
                   $jzl = TechnLevelLogic::techn_add_level_jzl($day,$v2['id']);
                    //结束计算加钟比例

    				//如果技师实时数据存在
    				//检查升级条件
                    $techn_level_map = [
                        'is_delete_time' => 0,
                        'low_result' => ['<=', $techn_last_info['total_result']],
                        'online_duration' => ['<=', $techn_last_info['total_duration']],
                        'plus_clock_rate' => ['<=', $jzl],
                        'integral' => ['<=', $techn_last_info['total_integral']],
                        'proxy_id'=>$v['proxy_id']
                    ];
                    $techn_level_info = Db::name('techn_level')->where($techn_level_map)->order('rank desc')->find();


                    if (empty($techn_level_info)){
                        $techn_level_map = [
                            'is_delete_time' => 0,
                            'low_result' => ['<=', $techn_last_info['total_result']],
                            'online_duration' => ['<=', $techn_last_info['total_duration']],
                            'plus_clock_rate' => ['<=', $jzl],
                            'integral' => ['<=', $techn_last_info['total_integral']],
                            'proxy_id'=>0
                        ];
                        $techn_level_info = Db::name('techn_level')->where($techn_level_map)->order('rank desc')->find();


                    }

//    				dump($techn_level_info);die;

    				if(!empty($techn_level_info))
    				{
    					$data = [
    						'nature_rank' => $techn_level_info['rank'] > $techn_last_info['nature_rank'] ? $techn_level_info['rank'] : $techn_last_info['nature_rank'],
    						'divide_rank' => $techn_level_info['rank'] > $techn_last_info['divide_rank'] ? $techn_level_info['rank'] : $techn_last_info['divide_rank'],
    						'upd_time' => time(),
    						'divide_into'=>$techn_level_info['divide_into'],
    						'jz_bl'=>$techn_level_info['jz_bl'],
    						'level_id'=>$techn_level_info['id'],
    						'total_result'=>0,
    						'total_duration'=>0,
    						'total_integral'=>0
    					];
    					//更新用户等级
    				}else{
                        $data = [
//                            'nature_rank' => $techn_level_info['rank'] > $techn_last_info['nature_rank'] ? $techn_level_info['rank'] : $techn_last_info['nature_rank'],
//                            'divide_rank' => $techn_level_info['rank'] > $techn_last_info['divide_rank'] ? $techn_level_info['rank'] : $techn_last_info['divide_rank'],
                            'upd_time' => time(),
                            'divide_into'=>60,
//                            'jz_bl'=>$techn_level_info['jz_bl'],
//                            'level_id'=>$techn_level_info['id'],
                            'total_result'=>0,
                            'total_duration'=>0,
                            'total_integral'=>0
                        ];
                        //更新用户等级
                    }
                    Db::name('techn_last')->where(['techn_id' => $v2['id']])->update($data);

	                
	            }
	            
	        }
	        
	    }
	}
	
	
    
    
    
	public function index2()
	{
	    
	    
	    $list=db::name('proxy')->select();
	    
	    if(!empty($list)){
	        
	        foreach ($list as $k=>$v){
	            $techn=db::name('techn')->where('city',$v['shi'])->select();
	            
	            foreach($techn as $k2=>$v2){
	                
	                $techn_last_info = Db::name('techn_last')->where(['techn_id' => $v2['id']])->find();
	                
    				//如果技师实时数据存在
    				//检查升级条件
    				$techn_level_map = [
    					'is_delete_time' => 0,
    					'low_result' => ['elt', $techn_last_info['total_result']],
    					'online_duration' => ['elt', $techn_last_info['total_duration']],
    					'plus_clock_price' => ['elt', $techn_last_info['total_plus_clock_price']],
    					'integral' => ['elt', $techn_last_info['total_integral']],
    					'proxy_id'=>$v['proxy_id']
    				];
    				$techn_level_info = Db::name('techn_level')->where($techn_level_map)->order('rank desc')->find();
    				
    				if(empty($techn_level_info)){
    				    $techn_level_map = [
        					'is_delete_time' => 0,
        					'low_result' => ['elt', $techn_last_info['total_result']],
        					'online_duration' => ['elt', $techn_last_info['total_duration']],
        					'plus_clock_price' => ['elt', $techn_last_info['total_plus_clock_price']],
        					'integral' => ['elt', $techn_last_info['total_integral']],
        					'proxy_id'=>0
        				];
        				$techn_level_info = Db::name('techn_level')->where($techn_level_map)->order('rank desc')->find();
    				}
    				//dump($techn_level_info);
    				if(!empty($techn_level_info))
    				{
    					$data = [
    						'nature_rank' => $techn_level_info['rank'] > $techn_last_info['nature_rank'] ? $techn_level_info['rank'] : $techn_last_info['nature_rank'],
    						'divide_rank' => $techn_level_info['rank'] > $techn_last_info['divide_rank'] ? $techn_level_info['rank'] : $techn_last_info['divide_rank'],
    						'upd_time' => time(),
    						'divide_into'=>$techn_level_info['divide_into']
    					];
    					
	               // print_r($techn_level_info);exit;
    					//更新用户等级
    					Db::name('techn_last')->where(['techn_id' => $v2['id']])->update($data);
    				}
	                
	            }
	            
	        }
	        
	    }
	}
	
	
	
	public function order_close()
	{
	    
		//查询订单信息
		$order = Db::name('order')->where(['status' => '0','add_time'=>['<',time()-300]])->select();
		
            // 订单状态日志添加
        $status_array = OrderService::$status_array;
        //开启事务
        Db::startTrans();
		foreach ($order as $k=>$order_info){
		    $upd_data = [
                'status'        => 8,
                'close_time'   => time(),
                'upd_time'      => time(),
            ];
            if(!Db::name('order')->where(['id' => $order_info['id']])->update($upd_data))
            {
            	//事务回滚
            	Db::rollback();
                exit();
            }
            $order_status_history_id = Db::name('order_status_history')->insertGetId([
    			'order_id' => $order_info['id'],
    			'original_status' => $order_info['status'],
    			'new_status' => $upd_data['status'],
    			'msg' => '系统关闭['.$status_array[$order_info['status']]['name'].'-'.$status_array[$upd_data['status']]['name'].']',
    			'identity_type' => 'system',
    			'identity_id' => 0,
    			'add_time' => time()
    		]);
    		if($order_status_history_id <= 0)
    		{
    			Db::rollback();
                exit();
    		}
		}
		
        
		//提交事务
		Db::commit();
	}
	
	public function TechnPreTime(){
        
        $techn=db::name('techn')->select();
        
        foreach ($techn as $k=>$v){
            TechnService::TechnPreTimey($v['id']);
        }
        
    }
}